package elijah.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import elijah.base.R;
import elijah.base.binding.TextBindable;
import elijah.base.databinding.ViewEntryItemBinding;
import elijah.base.utils.DensityUtils;

public class EntryItemView extends BaseWidget<ViewEntryItemBinding> implements TextBindable {


    public EntryItemView(@NonNull Context context) {
        super(context);
    }

    public EntryItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initView(@Nullable AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.EntryItemView);
            String string = typedArray.getString(R.styleable.EntryItemView_headerText);
            binding.header.setText(string);
            int color = typedArray.getColor(R.styleable.EntryItemView_headerTextColor, binding.header.getCurrentTextColor());
            binding.header.setTextColor(color);
            binding.header.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.EntryItemView_headerTextSize, binding.header.getTextSize()));

            boolean enable = typedArray.getBoolean(R.styleable.EntryItemView_drawableEnable, true);
            if (!enable) {
                binding.header.setCompoundDrawables(null, null, null, null);
            }
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) binding.header.getLayoutParams();
            int space = typedArray.getDimensionPixelSize(R.styleable.EntryItemView_space, -1);
            if (space == -1 && !binding.header.getText().toString().endsWith("：") && !binding.header.getText().toString().endsWith("：")) {
                space = DensityUtils.dp2px(8);
            }
            layoutParams.rightMargin = space;
            binding.header.setLayoutParams(layoutParams);


            binding.content.setText(typedArray.getString(R.styleable.EntryItemView_contentText));
            binding.content.setTextColor(typedArray.getColor(R.styleable.EntryItemView_contentTextColor, binding.content.getCurrentTextColor()));
            binding.content.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.EntryItemView_contentTextSize, binding.content.getTextSize()));
            binding.content.setBackgroundColor(typedArray.getColor(R.styleable.EntryItemView_contentBgColor, Color.TRANSPARENT));
            binding.content.setMaxLines(typedArray.getInt(R.styleable.EntryItemView_maxLines, 2));
            typedArray.recycle();
        }

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup.LayoutParams layoutParams = binding.item.getLayoutParams();
        layoutParams.height = getMeasuredHeight();
        binding.item.setLayoutParams(layoutParams);
    }

    public void setContentText(String text) {
        binding.content.setText(text);


    }

    public void setContentTextColor(int color) {
        binding.content.setTextColor(color);
    }

    public void setContentBackgroundColor(int color) {
        binding.content.setBackgroundColor(color);
    }


    @Override
    public void setText(String text) {
        setContentText(text);
    }

    @Override
    public String getText() {
        return binding.content.getText().toString();
    }

    public TextView getContentView() {
        return binding.content;
    }
}
